package cn.yunhe.dao.impl;

import cn.yunhe.BaseDB;
import cn.yunhe.dao.IAdminDao;
import cn.yunhe.entity.Admin;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class AdminDao extends BaseDB implements IAdminDao {
    Connection conn = null;
    PreparedStatement psmt = null;
    ResultSet rs = null;

    @Override
    public boolean getAdmin(String username) {
        boolean flag = false;
        try {
            String sql = "select * from tb_admin where c_name='" + username+"'";
            conn = getConn();
            psmt = conn.prepareStatement(sql);
            rs = psmt.executeQuery();
            flag = rs.next();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public Admin login(String username, String password) {
        Admin admin = null;
        try {
            String sql = "select * from tb_admin where c_name=? and c_pass=?";
            conn = getConn();
            psmt = conn.prepareStatement(sql);
            psmt.setObject(1,username);
            psmt.setObject(2,password);
            rs = psmt.executeQuery();
            if (rs.next()){
                admin=new Admin(rs.getInt("c_id"), rs.getString("c_name"),rs.getString("c_pass"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return admin;
    }

    @Override
    public boolean addAdmin(String username, String password) {
        String sql = "insert into tb_admin(c_name,c_pass) values(?,?)";
        Object[] arr = {username, password};
        return updateSql2(sql, arr) > 0;
    }
}
